﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teigha.DatabaseServices;
using Teigha.Geometry;

namespace SRB.Tg.DrawingStandard
{
    public static class BlockPlot
    {
        /// <summary>
        /// 插入外部dwg块文件
        /// </summary>
        /// <param name="blockName">块文件名 包含路径</param>
        /// <param name="layerName">图层名称</param>
        /// <param name="isShow">是否显示创建图形文件</param>
        /// <param name="isCover">是否覆盖</param>

        public static void InsertBlockFunc(this Database db, string blockName, string layerName, Point3d point, ref Extents2d ext, double scale, double ang, bool isShow = true, bool isCover = true)
        {

            // 新建块文件数据库对象 读取块
            using (Database blockDb = new Database(false, true))
            {
                // 读取外部块 dwg
                blockDb.ReadDwgFile(blockName, System.IO.FileShare.Read, true, null);
                // 关闭文件
                blockDb.CloseInput(true);
                // 新建图层id
                ObjectId layId = ObjectId.Null;
                // 开启事务处理
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    // 指定插入块后的新名称
                    string newName = System.IO.Path.GetFileNameWithoutExtension(blockName);

                    BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                    if ((bt.Has(newName)) && isCover)
                    {
                        // 插入块 insert it as a new block
                        ObjectId idBtr = db.Insert(newName, blockDb, false);

                        if (isShow)
                        {
                            BlockTableRecord curBtr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

                            LayerTable lt = trans.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;

                            if (!lt.Has(layerName))
                            {
                                LayerTableRecord ltr = new LayerTableRecord();
                                ltr.Name = layerName;
                                layId = lt.Add(ltr);
                                trans.AddNewlyCreatedDBObject(ltr, true);
                            }
                            curBtr.LayoutId = layId;

                            using (BlockReference blockRef = new BlockReference(point, idBtr))
                            {
                                curBtr.AppendEntity(blockRef);
                                trans.AddNewlyCreatedDBObject(blockRef, true);
                                blockRef.TransformBy(Matrix3d.Scaling(scale, point)); //缩放块; 修改块比例
                                blockRef.Rotation = ang;
                                ext = ext.Add(new Extents2d(blockRef.Bounds.Value.MinPoint.C2D(), blockRef.Bounds.Value.MaxPoint.C2D()));

                            }
                        }
                    }
                    else if (!(bt.Has(newName)))
                    {
                        // 插入块 insert it as a new block
                        ObjectId idBtr = db.Insert(newName, blockDb, false);

                        if (isShow)
                        {
                            BlockTableRecord curBtr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;


                            LayerTable lt = trans.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;

                            if (!lt.Has(layerName))
                            {
                                LayerTableRecord ltr = new LayerTableRecord();
                                ltr.Name = layerName;
                                layId = lt.Add(ltr);
                                trans.AddNewlyCreatedDBObject(ltr, true);
                            }
                            curBtr.LayoutId = layId;

                            using (BlockReference blockRef = new BlockReference(point, idBtr))
                            {
                                curBtr.AppendEntity(blockRef);
                                trans.AddNewlyCreatedDBObject(blockRef, true);
                                blockRef.TransformBy(Matrix3d.Scaling(scale, point)); //缩放块; 修改块比例
                                blockRef.Rotation = ang;
                                ext = ext.Add(new Extents2d(blockRef.Bounds.Value.MinPoint.C2D(), blockRef.Bounds.Value.MaxPoint.C2D()));
                            }
                        }
                    }
                    else
                    {
                        ObjectId idBtr = db.Insert(newName+DateTime.Now.ToString("yyyyMMddHHmmss"), blockDb, false);

                        if (isShow)
                        {
                            BlockTableRecord curBtr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;


                            LayerTable lt = trans.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;

                            if (!lt.Has(layerName))
                            {
                                LayerTableRecord ltr = new LayerTableRecord();
                                ltr.Name = layerName;
                                layId = lt.Add(ltr);
                                trans.AddNewlyCreatedDBObject(ltr, true);
                            }
                            curBtr.LayoutId = layId;

                            using (BlockReference blockRef = new BlockReference(point, idBtr))
                            {
                                curBtr.AppendEntity(blockRef);
                                trans.AddNewlyCreatedDBObject(blockRef, true);
                                blockRef.TransformBy(Matrix3d.Scaling(scale, point)); //缩放块; 修改块比例
                                blockRef.Rotation = ang;
                                ext = ext.Add(new Extents2d(blockRef.Bounds.Value.MinPoint.C2D(), blockRef.Bounds.Value.MaxPoint.C2D()));
                            }
                        }
                    }
                    trans.Commit();
                }
            }
        }

        public static void InsertBlockByDwg(this Database db, string dwgPath,string blockName, string layerName, Point3d point, ref Extents2d ext, double scale, double ang, bool isShow = true, bool isCover = true)
        {

            // 新建块文件数据库对象 读取块
            using (Database blockDb = new Database(false, true))
            {
                // 读取外部块 dwg
                blockDb.ReadDwgFile(dwgPath, System.IO.FileShare.Read, true, null);
                // 关闭文件
                blockDb.CloseInput(true);
                // 新建图层id
                ObjectId layId = ObjectId.Null;
                // 开启事务处理
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    // 指定插入块后的新名称
                 

                    BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                    if ((bt.Has(blockName)) && isCover)
                    {
                        // 插入块 insert it as a new block
                        ObjectId idBtr = db.Insert(blockName, blockName + DateTime.Now.ToString("yyyyMMddHHmmss"), blockDb, false);

                        if (isShow)
                        {
                            BlockTableRecord curBtr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

                            LayerTable lt = trans.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;

                            if (!lt.Has(layerName))
                            {
                                LayerTableRecord ltr = new LayerTableRecord();
                                ltr.Name = layerName;
                                layId = lt.Add(ltr);
                                trans.AddNewlyCreatedDBObject(ltr, true);
                            }
                            curBtr.LayoutId = layId;

                            using (BlockReference blockRef = new BlockReference(point, idBtr))
                            {
                                curBtr.AppendEntity(blockRef);
                                trans.AddNewlyCreatedDBObject(blockRef, true);
                                blockRef.TransformBy(Matrix3d.Scaling(scale, point)); //缩放块; 修改块比例
                                blockRef.Rotation = ang;
                                ext = ext.Add(new Extents2d(blockRef.Bounds.Value.MinPoint.C2D(), blockRef.Bounds.Value.MaxPoint.C2D()));

                            }
                        }
                    }
                    else if (!(bt.Has(blockName)))
                    {
                        // 插入块 insert it as a new block
                        ObjectId idBtr = db.Insert(blockName, blockName + DateTime.Now.ToString("yyyyMMddHHmmss"), blockDb, false);

                        if (isShow)
                        {
                            BlockTableRecord curBtr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;


                            LayerTable lt = trans.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;

                            if (!lt.Has(layerName))
                            {
                                LayerTableRecord ltr = new LayerTableRecord();
                                ltr.Name = layerName;
                                layId = lt.Add(ltr);
                                trans.AddNewlyCreatedDBObject(ltr, true);
                            }
                            curBtr.LayoutId = layId;

                            using (BlockReference blockRef = new BlockReference(point, idBtr))
                            {
                                curBtr.AppendEntity(blockRef);
                                trans.AddNewlyCreatedDBObject(blockRef, true);
                                blockRef.TransformBy(Matrix3d.Scaling(scale, point)); //缩放块; 修改块比例
                                blockRef.Rotation = ang;
                                ext = ext.Add(new Extents2d(blockRef.Bounds.Value.MinPoint.C2D(), blockRef.Bounds.Value.MaxPoint.C2D()));
                            }
                        }
                    }
                    else
                    {
                        ObjectId idBtr = db.Insert(blockName, blockName + DateTime.Now.ToString("yyyyMMddHHmmss"), blockDb, false);

                        if (isShow)
                        {
                            BlockTableRecord curBtr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;


                            LayerTable lt = trans.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;

                            if (!lt.Has(layerName))
                            {
                                LayerTableRecord ltr = new LayerTableRecord();
                                ltr.Name = layerName;
                                layId = lt.Add(ltr);
                                trans.AddNewlyCreatedDBObject(ltr, true);
                            }
                            curBtr.LayoutId = layId;

                            using (BlockReference blockRef = new BlockReference(point, idBtr))
                            {
                                curBtr.AppendEntity(blockRef);
                                trans.AddNewlyCreatedDBObject(blockRef, true);
                                blockRef.TransformBy(Matrix3d.Scaling(scale, point)); //缩放块; 修改块比例
                                blockRef.Rotation = ang;
                                ext = ext.Add(new Extents2d(blockRef.Bounds.Value.MinPoint.C2D(), blockRef.Bounds.Value.MaxPoint.C2D()));
                            }
                        }
                    }
                    trans.Commit();
                }
            }
        }

        public static void InsertBlockToPaperByDwg(this Database db, string dwgPath, string blockName, string layerName, Point3d point,  double scale, double ang, bool isShow = true, bool isCover = true)
        {

            // 新建块文件数据库对象 读取块
            using (Database blockDb = new Database(false, true))
            {
                // 读取外部块 dwg
                blockDb.ReadDwgFile(dwgPath, System.IO.FileShare.Read, true, null);
                // 关闭文件
                blockDb.CloseInput(true);
                // 新建图层id
                ObjectId layId = ObjectId.Null;
                // 开启事务处理
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    // 指定插入块后的新名称


                    BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    BlockTableRecord curBtr = trans.GetObject(bt[BlockTableRecord.PaperSpace], OpenMode.ForWrite) as BlockTableRecord;
                    if ((bt.Has(blockName)) && isCover)
                    {
                        // 插入块 insert it as a new block
                        ObjectId idBtr = db.Insert(blockName, blockName + DateTime.Now.ToString("yyyyMMddHHmmss"), blockDb, false);

                        if (isShow)
                        {
                            //BlockTableRecord curBtr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                         
                            LayerTable lt = trans.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;

                            if (!lt.Has(layerName))
                            {
                                LayerTableRecord ltr = new LayerTableRecord();
                                ltr.Name = layerName;
                                layId = lt.Add(ltr);
                                trans.AddNewlyCreatedDBObject(ltr, true);
                            }
                            curBtr.LayoutId = layId;

                            using (BlockReference blockRef = new BlockReference(point, idBtr))
                            {
                                curBtr.AppendEntity(blockRef);
                                trans.AddNewlyCreatedDBObject(blockRef, true);
                                blockRef.TransformBy(Matrix3d.Scaling(scale, point)); //缩放块; 修改块比例
                                blockRef.Rotation = ang;
                            
                            }
                        }
                    }
                    else if (!(bt.Has(blockName)))
                    {
                        // 插入块 insert it as a new block
                        ObjectId idBtr = db.Insert(blockName, blockName + DateTime.Now.ToString("yyyyMMddHHmmss"), blockDb, false);

                        if (isShow)
                        {
                            //BlockTableRecord curBtr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;


                            LayerTable lt = trans.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;

                            if (!lt.Has(layerName))
                            {
                                LayerTableRecord ltr = new LayerTableRecord();
                                ltr.Name = layerName;
                                layId = lt.Add(ltr);
                                trans.AddNewlyCreatedDBObject(ltr, true);
                            }
                            curBtr.LayoutId = layId;

                            using (BlockReference blockRef = new BlockReference(point, idBtr))
                            {
                                curBtr.AppendEntity(blockRef);
                                trans.AddNewlyCreatedDBObject(blockRef, true);
                                blockRef.TransformBy(Matrix3d.Scaling(scale, point)); //缩放块; 修改块比例
                                blockRef.Rotation = ang;
                               
                            }
                        }
                    }
                    else
                    {
                        ObjectId idBtr = db.Insert(blockName, blockName + DateTime.Now.ToString("yyyyMMddHHmmss"), blockDb, false);

                        if (isShow)
                        {
                            //BlockTableRecord curBtr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;


                            LayerTable lt = trans.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;

                            if (!lt.Has(layerName))
                            {
                                LayerTableRecord ltr = new LayerTableRecord();
                                ltr.Name = layerName;
                                layId = lt.Add(ltr);
                                trans.AddNewlyCreatedDBObject(ltr, true);
                            }
                            curBtr.LayoutId = layId;

                            using (BlockReference blockRef = new BlockReference(point, idBtr))
                            {
                                curBtr.AppendEntity(blockRef);
                                trans.AddNewlyCreatedDBObject(blockRef, true);
                                blockRef.TransformBy(Matrix3d.Scaling(scale, point)); //缩放块; 修改块比例
                                blockRef.Rotation = ang;
                                
                            }
                        }
                    }
                    trans.Commit();
                }
            }
        }


        public static void InsertBlockByDwgWithPara(this Database db, string dwgPath, string blockName, string layerName, Point3d point, ref Extents2d ext, double scale, double ang, Dictionary<string, Dictionary<string, string>> attNameValues, bool isShow = true, bool isCover = true)
        {

            // 新建块文件数据库对象 读取块
            using (Database blockDb = new Database(false, true))
            {
                // 读取外部块 dwg
                blockDb.ReadDwgFile(dwgPath, System.IO.FileShare.Read, true, null);
                // 关闭文件
                blockDb.CloseInput(true);
                // 新建图层id
                ObjectId layId = ObjectId.Null;
                // 开启事务处理
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    // 指定插入块后的新名称


                    BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                    if ((bt.Has(blockName)) && isCover)
                    {
                        // 插入块 insert it as a new block
                        ObjectId idBtr = db.Insert(blockName, blockName + DateTime.Now.ToString("yyyyMMddHHmmss"), blockDb, false);

                        if (isShow)
                        {
                            BlockTableRecord curBtr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

                            LayerTable lt = trans.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;

                            if (!lt.Has(layerName))
                            {
                                LayerTableRecord ltr = new LayerTableRecord();
                                ltr.Name = layerName;
                                layId = lt.Add(ltr);
                                trans.AddNewlyCreatedDBObject(ltr, true);
                            }
                            curBtr.LayoutId = layId;

                            using (BlockReference blockRef = new BlockReference(point, idBtr))
                            {
                                curBtr.AppendEntity(blockRef);
                                trans.AddNewlyCreatedDBObject(blockRef, true);
                                blockRef.TransformBy(Matrix3d.Scaling(scale, point)); //缩放块; 修改块比例
                                blockRef.Rotation = ang;
                                ext = ext.Add(new Extents2d(blockRef.Bounds.Value.MinPoint.C2D(), blockRef.Bounds.Value.MaxPoint.C2D()));
                                ChangeDynamicValue(blockRef, attNameValues);
                                //判断块表记录是否包含属性定义
                                if (curBtr.HasAttributeDefinitions)
                                {
                                    //若包含，则遍历属性定义
                                    foreach (ObjectId id in curBtr)
                                    {
                                        //检查是否是属性定义
                                        AttributeDefinition attDef = id.GetObject(OpenMode.ForRead) as AttributeDefinition;
                                        if (attDef != null)
                                        {

                                            //创建一个新的属性对象
                                            AttributeReference attribute = new AttributeReference();
                                            //从属性定义获取属性对象的对象特性
                                            attribute.SetAttributeFromBlock(attDef, blockRef.BlockTransform);
                                            attribute.Rotation = attDef.Rotation;
                                            attribute.Position = attDef.Position.TransformBy(blockRef.BlockTransform);
                                            attribute.AdjustAlignment(db);
                                            //判断是否包含指定的属性名称
                                            if (attNameValues.ContainsKey(attDef.Tag.ToUpper()))
                                            {
                                                //设置属性值
                                                attribute.TextString = attNameValues[attDef.Tag.ToUpper()].ToString();
                                            }
                                            // 向块参照添加属性对象
                                            blockRef.AttributeCollection.AppendAttribute(attribute);
                                            db.TransactionManager.AddNewlyCreatedDBObject(attribute, true);

                                        }
                                    }
                                }
                            }
                        }
                    }
                    else if (!(bt.Has(blockName)))
                    {
                        // 插入块 insert it as a new block
                        ObjectId idBtr = db.Insert(blockName, blockName + DateTime.Now.ToString("yyyyMMddHHmmss"), blockDb, false);

                        if (isShow)
                        {
                            BlockTableRecord curBtr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;


                            LayerTable lt = trans.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;

                            if (!lt.Has(layerName))
                            {
                                LayerTableRecord ltr = new LayerTableRecord();
                                ltr.Name = layerName;
                                layId = lt.Add(ltr);
                                trans.AddNewlyCreatedDBObject(ltr, true);
                            }
                            curBtr.LayoutId = layId;

                            using (BlockReference blockRef = new BlockReference(point, idBtr))
                            {
                                curBtr.AppendEntity(blockRef);
                                trans.AddNewlyCreatedDBObject(blockRef, true);
                                blockRef.TransformBy(Matrix3d.Scaling(scale, point)); //缩放块; 修改块比例
                                blockRef.Rotation = ang;
                                ext = ext.Add(new Extents2d(blockRef.Bounds.Value.MinPoint.C2D(), blockRef.Bounds.Value.MaxPoint.C2D()));
                                ChangeDynamicValue(blockRef, attNameValues);
                                //判断块表记录是否包含属性定义
                                if (curBtr.HasAttributeDefinitions)
                                {
                                    //若包含，则遍历属性定义
                                    foreach (ObjectId id in curBtr)
                                    {
                                        //检查是否是属性定义
                                        AttributeDefinition attDef = id.GetObject(OpenMode.ForRead) as AttributeDefinition;
                                        if (attDef != null)
                                        {

                                            //创建一个新的属性对象
                                            AttributeReference attribute = new AttributeReference();
                                            //从属性定义获取属性对象的对象特性
                                            attribute.SetAttributeFromBlock(attDef, blockRef.BlockTransform);
                                            attribute.Rotation = attDef.Rotation;
                                            attribute.Position = attDef.Position.TransformBy(blockRef.BlockTransform);
                                            attribute.AdjustAlignment(db);
                                            //判断是否包含指定的属性名称
                                            if (attNameValues.ContainsKey(attDef.Tag.ToUpper()))
                                            {
                                                //设置属性值
                                                attribute.TextString = attNameValues[attDef.Tag.ToUpper()].ToString();
                                            }
                                            // 向块参照添加属性对象
                                            blockRef.AttributeCollection.AppendAttribute(attribute);
                                            db.TransactionManager.AddNewlyCreatedDBObject(attribute, true);

                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        ObjectId idBtr = db.Insert(blockName, blockName + DateTime.Now.ToString("yyyyMMddHHmmss"), blockDb, false);

                        if (isShow)
                        {
                            BlockTableRecord curBtr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;


                            LayerTable lt = trans.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;

                            if (!lt.Has(layerName))
                            {
                                LayerTableRecord ltr = new LayerTableRecord();
                                ltr.Name = layerName;
                                layId = lt.Add(ltr);
                                trans.AddNewlyCreatedDBObject(ltr, true);
                            }
                            curBtr.LayoutId = layId;

                            using (BlockReference blockRef = new BlockReference(point, idBtr))
                            {
                                curBtr.AppendEntity(blockRef);
                                trans.AddNewlyCreatedDBObject(blockRef, true);
                                blockRef.TransformBy(Matrix3d.Scaling(scale, point)); //缩放块; 修改块比例
                                blockRef.Rotation = ang;
                                ext = ext.Add(new Extents2d(blockRef.Bounds.Value.MinPoint.C2D(), blockRef.Bounds.Value.MaxPoint.C2D()));
                                ChangeDynamicValue(blockRef, attNameValues);
                                //判断块表记录是否包含属性定义
                                if (curBtr.HasAttributeDefinitions)
                                {
                                    //若包含，则遍历属性定义
                                    foreach (ObjectId id in curBtr)
                                    {
                                        //检查是否是属性定义
                                        AttributeDefinition attDef = id.GetObject(OpenMode.ForRead) as AttributeDefinition;
                                        if (attDef != null)
                                        {

                                            //创建一个新的属性对象
                                            AttributeReference attribute = new AttributeReference();
                                            //从属性定义获取属性对象的对象特性
                                            attribute.SetAttributeFromBlock(attDef, blockRef.BlockTransform);
                                            attribute.Rotation = attDef.Rotation;
                                            attribute.Position = attDef.Position.TransformBy(blockRef.BlockTransform);
                                            attribute.AdjustAlignment(db);
                                            //判断是否包含指定的属性名称
                                            if (attNameValues.ContainsKey(attDef.Tag.ToUpper()))
                                            {
                                                //设置属性值
                                                attribute.TextString = attNameValues[attDef.Tag.ToUpper()].ToString();
                                            }
                                            // 向块参照添加属性对象
                                            blockRef.AttributeCollection.AppendAttribute(attribute);
                                            db.TransactionManager.AddNewlyCreatedDBObject(attribute, true);

                                        }
                                    }
                                }
                            }
                        }
                    }
                    trans.Commit();
                }
            }
        }

        public static void ChangeDynamicValue(BlockReference br, Dictionary<string, Dictionary<string, string>> attDNameValues)
        {
            if (br != null && br.IsDynamicBlock)
            {
                DynamicBlockReferencePropertyCollection pc = br.DynamicBlockReferencePropertyCollection;
                AttributeCollection ar = br.AttributeCollection;

                foreach (DynamicBlockReferenceProperty prop in pc)
                {
                    if (attDNameValues.Keys.Contains(prop.PropertyName) && !prop.ReadOnly)
                    {
                        try
                        {
                            Dictionary<string, string> dic = attDNameValues[prop.PropertyName];
                            List<string> values = dic.Keys.ToList();
                            List<string> types = dic.Values.ToList();
                            switch (types[0].ToLower())
                            {
                                case "double":
                                    string[] sp = prop.PropertyName.Split('_');
                                    if (sp != null)
                                    {
                                        //if(sp[sp.Length-1]=="mm")
                                        //    prop.Value = double.Parse(values[0])/1000;
                                        //else
                                        prop.Value = double.Parse(values[0]);
                                    }
                                    break;
                                case "string":
                                    prop.Value = values[0];
                                    break;
                            }
                            //prop.Value = attDNameValues[prop.PropertyName];

                        }
                        catch (System.Exception ex)
                        {

                        }
                    }
                    else
                    {
                        continue;
                    }

                }
            }

        }
    }
}
